package oocl.eshop.ui.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;

import oocl.eshop.po.Merchant;
import oocl.eshop.service.MerchantManager;
import oocl.eshop.service.impl.MerchantManagerImpl;
import oocl.eshop.util.jms.JmsSendXML;
import oocl.eshop.util.regular.DataMatching;

/**
 * Servlet Filter implementation class MerchantEleFilter
 */
public class MerchantEleFilter implements Filter {
	MerchantManager merchantManager = new MerchantManagerImpl();

	/**
	 * Default constructor.
	 */
	public MerchantEleFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request2 = (HttpServletRequest) request;
		HttpSession session = request2.getSession();
		// fileupload的参数设置对象
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 300);
		// fileupload的工作核心对象
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		// fileItem封装各个表单项，每个fileitem是一个input
		try {
			List<FileItem> fileItems = fileUpload.parseRequest(new ServletRequestContext(request2));
			Merchant merchant = merchantManager.LoadFileMerchant(fileItems,(Merchant)session.getAttribute("merchant"));
			if (session.getAttribute("merchant") != null) {
				Merchant merchant2 = (Merchant) session.getAttribute("merchant");
				if (merchant.getmBrand() != null)
					merchant2.setmBrand(merchant.getmBrand());
				if (merchant.getmLocation() != null)
					merchant2.setmLocation(merchant.getmLocation());
				if (merchant.getmLogoPath() != null)
					merchant2.setmLogoPath(merchant.getmLogoPath());
				if (merchant.getmPassword() != null)
					merchant2.setmPassword(merchant.getmPassword());
				merchant = merchant2;
			}
			// 匹配电话
			if (!DataMatching.isTel(merchant.getmTel())) {
				request.setAttribute("Error", "Tel format Error.");
			}
			// 匹配身份证
			if (!DataMatching.isIdCard(merchant.getmIdCard())) {
				request.setAttribute("Error", "Id card format Error.");
			}
			// 匹配姓名格式
			if (!DataMatching.isName(merchant.getmPersonName())) {
				request.setAttribute("Error", "Person name should be letter between 3 to 20.");
			}
			if (!DataMatching.isPassword(merchant.getmPassword())) {
				request.setAttribute("Error", "Password should be letter between 6 to 30.");
			}
			if (request.getAttribute("Error") == null) {
				request.setAttribute("thisMerchant", merchant);	
				chain.doFilter(request, response);

			}else {

				request.getRequestDispatcher("register.jsp").forward(request, response);
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
